﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FirstTry.Model;

namespace FirstTry.DB
{
    class DBUser
    {
        DBConnection dbc = new DBConnection();


        public void addUser(User u)
        {
            string query = "INSERT INTO Users (fName, lName, address, zipCode, city, licensPlate, phoneNumber, email, userName, password, rank) VALUES (@fName, @lName, @address, @zipCode, @city, @licensPlate, @phoneNumber, @email, @userName, @password, @rank)";
            SqlConnection con = dbc.GetConnection();
            SqlCommand cmd = new SqlCommand(query, con);

            cmd.Parameters.AddWithValue("@fName", u.fName);
            cmd.Parameters.AddWithValue("@lName", u.lName);
            cmd.Parameters.AddWithValue("@address", u.address);
            cmd.Parameters.AddWithValue("@zipCode", u.zipCode);
            cmd.Parameters.AddWithValue("@city", u.city);
            cmd.Parameters.AddWithValue("@licensPlate", u.licensPlate);
            cmd.Parameters.AddWithValue("@phoneNumber", u.phoneNumber);
            cmd.Parameters.AddWithValue("@email", u.email);
            cmd.Parameters.AddWithValue("@userName", u.userName);
            cmd.Parameters.AddWithValue("@password", u.password);
            cmd.Parameters.AddWithValue("@rank", u.rank);

            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (SqlException e)
            {
                Console.WriteLine(e.Message);
            }
        }




        public User getUserByID(String ID)
        {
            SqlConnection con = dbc.GetConnection();
            string query = "SELECT ID, fName, lName, address, zipCode, city, licensPlate, phoneNumber, email, userName, password, rank FROM Users WHERE ID =" + ID;
            SqlCommand cmd = new SqlCommand(query, con);
            SqlDataReader dr = cmd.ExecuteReader();
            User u = new User();
            while (dr.Read())
            {

                u.ID = Convert.ToInt32(dr["ID"]);
                u.fName = dr["fName"].ToString();
                u.lName = dr["lName"].ToString();
                u.address = dr["address"].ToString();
                u.zipCode = Convert.ToInt32(dr["zipCode"]);
                u.city = dr["city"].ToString();
                u.licensPlate = dr["licensPlate"].ToString();
                u.phoneNumber = dr["phoneNumber"].ToString();
                u.email = dr["email"].ToString();
                u.userName = dr["userName"].ToString();
                u.password = dr["password"].ToString();
                u.rank = Convert.ToInt32(dr["rank"]);

            }
            return u;
        }
    }
}
        
      

